package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.CommentMedia;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.service.ICommentMediaService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 用户评论媒体文件
 * </p>
 *
 * @author chenxuebin
 * @since 2018-08-22
 */
@RestController
public class CommentMediaController {

    private static final Logger logger = LoggerFactory.getLogger(CommentMediaController.class);

    @Autowired
    private ICommentMediaService commentmediaService;

    @RequestMapping("/api/commentmedia/list")
    @ResponseBody
    public Jinfo list(@RequestParam(value = "status", required = false) String status,
                      @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                      @RequestParam(value = "pageSize", required = false) Integer pageSize,
                      HttpServletRequest req) {
        Jinfo info = new Jinfo();
        if (pageSize == null) pageSize = 10;
        Page page = new Page(pageIndex, pageSize);
        page = commentmediaService.selectPage(page, new EntityWrapper<CommentMedia>()
                .eq("uid", ObjectUtil.getUid())
                .eq("state", 1));
        info.setInfo(page.getRecords());
        return info;
    }

    @RequestMapping("/api/commentmedia/add")
    @ResponseBody
    public Jinfo commentmediaAdd(@Valid CommentMedia commentmedia,
                                 BindingResult bindingResult, HttpServletRequest req) {
        Jinfo info = new Jinfo();
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            info.setMsg("There are errors! " + bindingResult);
            info.setCode(400);
            return info;
        }
        commentmedia.setCreatetime(new Date());
        commentmedia.insert();
        return info;
    }

    @RequestMapping("/api/commentmedia/modify")
    @ResponseBody
    public Jinfo commentmediaUpdate(HttpServletRequest req,
                                    CommentMedia commentmedia) {
        Jinfo info = new Jinfo();
        commentmedia.setModifytime(new Date());
        commentmedia.updateById();
        return info;
    }

    @RequestMapping("/api/commentmedia/detail")
    @ResponseBody
    public Jinfo getCommentMediaDetailById(@RequestParam("token") String token,
                                           @RequestParam("id") long id, HttpServletRequest req
    ) {
        Jinfo info = new Jinfo();
        CommentMedia commentmedia = commentmediaService.selectById(id);
        info.setInfo(commentmedia);
        return info;
    }

}
